home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 02 Evans / Listing1.cpp
C/C++ Source or Header  |  2001-12-09  |  1KB  |  58 lines

  1. /* Copyright (C) Richard Evans, 2001. 
  2.  * All rights reserved worldwide.
  3.  *
  4.  * This software is provided "as is" without express or implied
  5.  * warranties. You may freely copy and compile this source into
  6.  * applications you distribute provided that the copyright text
  7.  * below is included in the resulting source code, for example:
  8.  * "Portions Copyright (C) Richard Evans, 2001"
  9.  */
  10.  
  11. // So we need another way of defining objects, one which makes 
  12. // it easy for us to iterate through the attributes:
  13.  
  14. class Attribute        
  15. {
  16. public:
  17.     virtual int GetValueRange()=0
  18.     virtual int GetValue()=0
  19.     virtual char* GetName()=0
  20. };
  21.  
  22. class Health : public Attribute
  23. {
  24.     ...
  25. };
  26.  
  27. class Energy : public Attribute
  28. {
  29.     ...
  30. };
  31.  
  32. class Object
  33. {
  34. protected:
  35.     Attribute**    Attributes;
  36. public:
  37.     virtual int GetNumAttributes()=0;    
  38.     ...
  39. };
  40.  
  41. class Villager : public Object
  42. {
  43. public:
  44.     int GetNumAttributes();
  45.     ...
  46. };
  47.  
  48. // Now it is easy to iterate through the attributes:
  49.  
  50. void Object::IterateThroughAttributes(void (*function)(Attribute*))
  51. {
  52.     for(int i=0; i<GetNumAttributes(); i++)
  53.     {
  54.         function(Attributes[i]);
  55.     }
  56. }
  57.  
  58.